#!/bin/bash

DIR="./"
SUBDIR="programs/"
STR_OK='\e[1m\e[32m[ OK ]\e[0m'
STR_BAD='\e[1m\e[31m[ BAD ]\e[0m'
STR_INFO='\e[1m\e[34m[ INFO ]\e[0m'
STR_WARN='\e[1m\e[33m[ WARN ]\e[0m'
STR_QUES='\e[1m\e[35m[ QUES ]\e[0m'
COMMAND=
ASK=

function ynq {
    echo -e -n $STR_QUES $@ "\e[1m[y/N]\e[0m "
    read reply </dev/tty
    if echo $reply | grep -i -q y; then
	return 0
    fi
    return 1
}

function init {
    cloned=true
    initted=true
    if [[ -a $DIR$SUBDIR$1 ]]; then
	cloned=
	echo -e $STR_WARN $1 Already cloned
	if [[ $ASK ]] && ynq "Do you want to clone it again ?"; then
	    cloned=true
	    rm -rf $DIR$SUBDIR$1
	fi
    fi

    if [[ -a $DIR.slsl/$1 ]]; then
	initted=
	echo -e $STR_WARN $1 Already initialized
	if [[ $ASK ]] && ynq "Do you want to initialize it again ?"; then
	    initted=true
	    rm -rf $DIR.slsl/$1
	fi
    fi
    
    if [[ $initted ]]; then
	echo -e -n $STR_INFO Initializing $1" "
	mkdir $DIR.slsl/$1/patches -p
	mkdir $DIR.slsl/$1/diffs -p
	touch $DIR.slsl/$1/diffs/config.h.diff
	echo -e $STR_OK
    fi

    if [[ $cloned ]]; then
	echo -e $STR_INFO Cloning $1" "
	mkdir $DIR$SUBDIR$1 -p
	git clone https://git.suckless.org/$1 $DIR$SUBDIR$1 --depth 1
	echo -e $STR_OK
    fi
	
	
}

function ptt {
    ls -1A $DIR.slsl/$1/patches | while IFS= read -r pat; do
	echo -e -n $STR_INFO Patching $1 with $pat" "
	cd $DIR$SUBDIR$1
	patch -Np1 -i ../../.slsl/$1/patches/$pat
	cd -
	echo -e $STR_OK
    done
}

function install {
    applyConfig $1
    cd $DIR$SUBDIR$1
    echo -e $STR_INFO Installing $1
    echo ~
    sudo make install
    sudo make clean
    cd -
    rm -rf $DIR$SUBDIR$1/config.h
}

function pull {
    echo -e $STR_INFO Pulling $1
    cd $DIR$SUBDIR$1
    git pull
    cd -
    echo -e $STR_OK
}

function applyConfig {
    cd $DIR$SUBDIR$1
    cp config.def.h config.h
    patch config.h ../../.slsl/$1/diffs/config.h.diff
    cd -
}

function configure {
    echo -e $STR_INFO Enabling config mode for $1
    applyConfig $1
    cp $DIR$SUBDIR$1/config.h $DIR"$1_tmp.h"
    rm -f $DIR$SUBDIR$1/config.h
    echo -e $STR_INFO Apply your changes to files in $DIR$1"_tmp.h"
}

function checkout {
    echo -e -n $STR_INFO Generating diff" "
    cd $DIR$SUBDIR$1
    if [[ -a ../../$1"_tmp.h" ]]; then
	diff config.def.h ../../$1"_tmp.h" > ../../.slsl/$1/diffs/config.h.diff
    else
	echo -e $STR_BAD Not in configuration mode
	cd -
	exit 0
    fi
    cd -
    echo -e $STR_OK
    echo -e -n $STR_INFO Removing temp file" "
    rm -rf $DIR$1"_tmp.h"
    echo -e $STR_OK
}

if echo $@ | grep -q -- "--ask"; then
    ASK=true
fi

if [[ $1 = "init" ]]; then
    COMMAND=init
elif [[ $1 = "patch" ]]; then
    COMMAND=ptt
elif [[ $1 = "install" ]]; then
    COMMAND=install
elif [[ $1 = "pull" ]]; then
    COMMAND=pull
elif [[ $1 = "configure" ]]; then
    COMMAND=configure
elif [[ $1 = "checkout" ]]; then
    COMMAND=checkout
elif [[ $1 = "apply" ]]; then
    COMMAND=applyConfig
else
    echo -e $STR_BAD No correct command
    exit
fi


if [[ $2 ]]; then
    programs="$(echo "$2" | sed 's/\ /\n/g')"
    if [[ "$2" = "all" ]]; then
	programs="$(ls -1A $SUBDIR)"
    fi
    while IFS= read -r name; do
	$COMMAND $name
    done <<<"$programs"
else
    echo -e $STR_BAD No program name inputted
fi
